<?php

	global $sl_settings;
	$sl_settings = array(
				'image_position'=>'center',
				'image_width'=>400,
				'image_height'=>300,
				'slide_width'=>400,
				'slide_height'=>300,
				'show_title'=>false, 
				'show_description'=>false, 
				'nav_in_slide'=>true,
				'slider_type'=>'jQuery Cycle',
				'transition_speed'=>2000,
				'slide_duration'=>2000,
				'slide_quantity'=>5,
				'cycle_transition_style'=>'fade',				
				'nivo_transition_style'=>'random',
				'nivo_theme'=>'theme-default',
				'slideshow_navigation'=>array('directionNav'=>false, 'controlNav'=>false),
				'specific_slideshow'=>''
				);	
	
	function sl_load_settings() {
		global $sl_settings;
		$items = get_theme_support( 'cvincludes-slides' );
		if(isset($items[0])) {
			 foreach ($items[0] as $setting_id => $setting_value)
				$sl_settings[$setting_id] = $setting_value;
		}
		
		$data = cv_get_option();
		$sl_settings['slider_type'] = $data['sl_slider_type'];
		$sl_settings['transition_speed'] = $data['sl_transition_speed'];
		$sl_settings['slide_duration'] = $data['sl_slide_duration'];
		$sl_settings['slide_quantity'] = $data['sl_slide_quantity'];
		$sl_settings['cycle_transition_style'] = $data['sl_cycle_transition_style'];
		$sl_settings['nivo_transition_style'] = $data['sl_nivo_transition_style'];
//		$sl_settings['nivo_theme'] = $data['sl_nivo_theme'];
		if(is_array($data['sl_slideshow_navigation'])) {
			$sl_settings['slideshow_navigation']['directionNav'] = in_array('Direction Nav', $data['sl_slideshow_navigation']['enabled'])? true:false;
			$sl_settings['slideshow_navigation']['controlNav'] = in_array('Control Nav', $data['sl_slideshow_navigation']['enabled'])? true:false;
		}
		$sl_settings['specific_slideshow'] = $data['sl_specific_slideshow'];				
	}	
	
	sl_load_settings();
	
	// Adds custom post type for Slides	
	add_action( 'init', 'sl_register_slides' );
	function sl_register_slides() {
	
		$labels = array(

			'name'               => __( 'Slides', hybrid_get_textdomain() ),
			'singular_name'      => __( 'Slide', hybrid_get_textdomain() ),
			'add_new'            => __( 'Add New', hybrid_get_textdomain() ),
			'add_new_item'       => __( 'Add New Slide', hybrid_get_textdomain() ),
			'edit_item'          => __( 'Edit Slide', hybrid_get_textdomain() ),
			'new_item'           => __( 'New Slide', hybrid_get_textdomain() ),
			'view_item'          => __( 'View Slide', hybrid_get_textdomain() ),
			'search_items'       => __( 'Search Slides', hybrid_get_textdomain() ),
			'not_found'          => __( 'No slides found', hybrid_get_textdomain() ),
			'not_found_in_trash' => __( 'No slides found in Trash', hybrid_get_textdomain() ), 
			'parent_item_colon'  => '',
			'menu_name'          => __( 'Slides', hybrid_get_textdomain() )

		);
				
		if ( function_exists( 'members_get_capabilities' ) ) {	
			$capabilities = array(
		
				'edit_post'          => 'sl_edit_slide',
				'edit_posts'         => 'sl_edit_slides',
				'edit_others_posts'  => 'sl_edit_others_slides',
				'publish_posts'      => 'sl_publish_slides',
				'read_post'          => 'sl_read_slide',
				'read_private_posts' => 'sl_read_private_slides',
				'delete_post'        => 'sl_delete_slide'

			);
			
			$capabilitytype = 'slide';
			
			$mapmetacap = false;
		
		} else {
		
			$capabilities = array(
		
				'edit_post'          => 'edit_post',
				'edit_posts'         => 'edit_posts',
				'edit_others_posts'  => 'edit_others_posts',
				'publish_posts'      => 'publish_posts',
				'read_post'          => 'read_post',
				'read_private_posts' => 'read_private_posts',
				'delete_post'        => 'delete_post'

			);
			
			$capabilitytype = 'post';
			
			$mapmetacap = true;
		
		}
		
		$args = array(
	
			'labels'              => $labels,
			'public'              => true,
			'publicly_queryable'  => false,
			'exclude_from_search' => true,
			'show_ui'             => true,
			'show_in_menu'        => true,
			'menu_icon'           => ''. trailingslashit(CVINCLUDES_SLIDES_URI) . 'images/slides-icon-20x20.png',
			'capability_type'     => $capabilitytype,
			'capabilities'        => $capabilities,
			'map_meta_cap'        => $mapmetacap,
			'hierarchical'        => false,
			'supports'            => array( 'title', 'thumbnail', 'editor' ),
			'taxonomies'          => array( 'slideshow' ),
			'has_archive'         => false,
			'rewrite'             => false,
			'query_var'           => true,
			'can_export'          => true,
			'show_in_nav_menus'   => false
		
		);
  
		register_post_type( 'slide', $args );
		
	}

	// Adds custom taxonomy for Slideshows
	
	add_action( 'init', 'sl_register_taxonomy' );	
	function sl_register_taxonomy() {
	
		$labels = array(
				
			'name'              => __( 'Slideshows', hybrid_get_textdomain() ),
			'singular_name'     => __( 'Slideshow', hybrid_get_textdomain() ),
			'search_items'      => __( 'Search Slideshows', hybrid_get_textdomain() ),
			'popular_items'     => __( 'Popular Slideshows', hybrid_get_textdomain() ),
			'all_items'         => __( 'All Slideshows', hybrid_get_textdomain() ),
			'parent_item'       => __( 'Parent Slideshow', hybrid_get_textdomain() ),
			'parent_item_colon' => __( 'Parent Slideshow:', hybrid_get_textdomain() ),
			'edit_item'         => __( 'Edit Slideshow', hybrid_get_textdomain() ),
			'update_item'       => __( 'Update Slideshow', hybrid_get_textdomain() ),
			'add_new_item'      => __( 'Add New Slideshow', hybrid_get_textdomain() ),
			'new_item_name'     => __( 'New Slideshow Name', hybrid_get_textdomain() ),
			'menu_name'         => __( 'Slideshows', hybrid_get_textdomain() )
				
		);
		
		if ( function_exists( 'members_get_capabilities' ) ) {
	
			$capabilities = array(
		
				'manage_terms' => 'sl_manage_slideshows',
				'edit_terms'   => 'sl_manage_slideshows',
				'delete_terms' => 'sl_manage_slideshows',
				'assign_terms' => 'sl_edit_slides'

			);
		
		} else {
		
			$capabilities = array(
		
				'manage_terms' => 'manage_categories',
				'edit_terms'   => 'manage_categories',
				'delete_terms' => 'manage_categories',
				'assign_terms' => 'edit_posts'

			);
		
		}
		
		$args = array(
	
			'labels'            => $labels,
			'public'            => true,
			'show_in_nav_menus' => false,
			'show_ui'           => true,
			'show_tagcloud'     => false,
			'hierarchical'      => true,
			'rewrite'           => array( 'slug' => 'slideshow' ),
			'capabilities'      => $capabilities
		
		);
	
		register_taxonomy( 'slideshow', 'slide', $args );
		
	}
	
	// Adds featured image functionality for Slides	
	add_action( 'after_setup_theme', 'sl_featured_image_array', '9999' );
	function sl_featured_image_array() {
	
		global $_wp_theme_features;

		if ( !isset( $_wp_theme_features['post-thumbnails'] ) ) {
		
			$_wp_theme_features['post-thumbnails'] = array( array( 'slide' ) );
			
		}

		elseif ( is_array( $_wp_theme_features['post-thumbnails'] ) ) {
        
			$_wp_theme_features['post-thumbnails'][0][] = 'slide';
			
		}
		
	}
	
	// Adds featured image size for Slides
	add_image_size( 'featured-slide', $sl_settings['image_width'], $sl_settings['image_height'], true );		
	add_image_size( 'featured-slide-thumb', 250, 9999 );	

	
	// Adds CSS for the slideshow
	add_action( 'wp_enqueue_scripts', 'sl_css' );
	function sl_css() {
		global $sl_settings;
		if( is_home() || is_front_page()) {
			if($sl_settings['slider_type'] == 'jQuery Cycle') 
				wp_enqueue_style( 'style-cycle', trailingslashit(CVINCLUDES_SLIDES_URI) . 'css/cycle-slides.css');
			else if($sl_settings['slider_type'] == 'Nivo Slider') {
				wp_enqueue_style( 'style-nivo', trailingslashit(CVINCLUDES_SLIDES_URI) . 'css/nivo-slides.css');
//				if($sl_settings['nivo_theme'] == 'theme-pascal')
//					$theme_path = 'pascal/pascal.css';
//				else if($sl_settings['nivo_theme'] == 'theme-orman')
//					$theme_path = 'orman/orman.css';
//				else 
					$theme_file = 'default/default.css';
				wp_enqueue_style( 'style-nivo-theme', trailingslashit(CVINCLUDES_SLIDES_URI) . 'css/nivo-themes/'. $theme_file);
			}
			else if($sl_settings['slider_type'] == 'liteAccordion') {
				wp_enqueue_style( 'style-accordion', trailingslashit(CVINCLUDES_SLIDES_URI) . 'css/liteaccordion/liteaccordion.css');
			}
		}
	}
	
	// Adds JavaScript for the slideshow	
	add_action( 'wp_print_scripts', 'sl_javascript' );			
	function sl_javascript() {
		global $sl_settings; 	
		 
		if( is_home() || is_front_page()) {				
			if($sl_settings['slider_type'] == 'jQuery Cycle') {					
				wp_enqueue_script( 'jquery-cycle', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/jquery.cycle.all.min.js', array( 'jquery' ) );
				wp_enqueue_script( 'jquery-metadata', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/jquery.metadata.v2.js', array( 'jquery' ) );			
					
				wp_enqueue_script( 'sl-cycle-script', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/cycle.slideshow.js', array('jquery', 'jquery-cycle') );							
				wp_localize_script( 'sl-cycle-script', 'cvslidessettings',			
					array(
					
						'cvslideshowspeed'      => $sl_settings['transition_speed'],
						'cvslideshowduration'   => $sl_settings['slide_duration'],
						'cvslideshowheight'     => $sl_settings['slide_height'],
						'cvslideshowwidth'      => $sl_settings['slide_width'],
						'cvslideshowtransition' => $sl_settings['cycle_transition_style']		
					)
					
				);
			}
			else if($sl_settings['slider_type'] == 'Nivo Slider') {		
				wp_enqueue_script( 'jquery-nivo', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/jquery.nivo.slider.pack.js', array( 'jquery' ) );
				wp_enqueue_script( 'sl-nivo-script', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/nivo.slideshow.js', array('jquery', 'jquery-nivo') );
				wp_localize_script('sl-nivo-script', 'cvslidessettings',			
					array(					
						'cvslideshowspeed'      => $sl_settings['transition_speed'],
						'cvslideshowduration'   => $sl_settings['slide_duration'],						
						'cvslideshowtransition' => $sl_settings['nivo_transition_style'],
						'cvslideshowdirection'  => $sl_settings['slideshow_navigation']['directionNav'], // Next & Prev navigation						
						'cvslideshowcontrol'    => $sl_settings['slideshow_navigation']['controlNav'] // 1,2,3... navigation
					)
					
				);
			}
			else if($sl_settings['slider_type'] == 'liteAccordion') {
				wp_enqueue_script( 'jquery-easing', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/jquery.easing.1.3.js', array( 'jquery' ) );
				wp_enqueue_script( 'jquery-accordion', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/liteaccordion.jquery.min.js', array( 'jquery' ) );
				wp_enqueue_script( 'sl-accordion-script', trailingslashit(CVINCLUDES_SLIDES_URI) . 'js/accordion.slideshow.js', array('jquery', 'jquery-accordion') );
				wp_localize_script( 'sl-accordion-script', 'cvslidessettings',			
					array(						
						'cvslideshowheight'     => $sl_settings['slide_height'],
						'cvslideshowwidth'      => $sl_settings['slide_width']					
					)
					
				);
			}
		}
	
	}
	
	// Load admin functions if in the backend
	
	if ( is_admin() ) {	
		require_once( 'lib/slides-admin.php' );	
	}
	
	
	// Adds function to load slideshow in theme
		
	function cv_slideshow( $metadata='' ) {
		global $sl_settings; 	
		if($sl_settings['slider_type'] == 'jQuery Cycle') 		
			include( 'lib/cycle-slideshow.php' );
		else if($sl_settings['slider_type'] == 'Nivo Slider')
			include( 'lib/nivo-slideshow.php' );
		else if($sl_settings['slider_type'] == 'liteAccordion')
			include( 'lib/accordion-slideshow .php' );
			
	}
		
?>